package com.studyv3.test.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.studyv3.common.core.page.PageQuery;
import com.studyv3.common.core.page.TableDataInfoPlus;
import com.studyv3.common.utils.StringUtils;
import com.studyv3.test.domain.StudyBookTypes;
import com.studyv3.test.domain.bo.StudyBookTypesBo;
import com.studyv3.test.domain.vo.StudyBookTypesVo;
import com.studyv3.test.mapper.StudyBookTypesMapper;
import com.studyv3.test.service.IStudyBookTypesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * (StudyBookTypes)表服务实现类
 *
 * @author makejava
 * @since 2024-04-25 16:00:59
 */
@Service
@RequiredArgsConstructor
public class StudyBookTypesServiceImpl extends ServiceImpl<StudyBookTypesMapper, StudyBookTypes> implements IStudyBookTypesService {
    private final StudyBookTypesMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public StudyBookTypesVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfoPlus<StudyBookTypesVo> queryPageList(StudyBookTypesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudyBookTypes> lqw = buildQueryWrapper(bo);
        int total = Math.toIntExact(count(lqw));

        int page = (pageQuery.getPageNum()-1)*pageQuery.getPageSize();
        lqw.last("limit " + page + "," + pageQuery.getPageSize());
        List<StudyBookTypesVo> result = baseMapper.selectVoList(lqw);

        TableDataInfoPlus<StudyBookTypesVo> build = TableDataInfoPlus.build(result);
        build.setTotal(total);
        return  build;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<StudyBookTypesVo> queryList(StudyBookTypesBo bo) {
        LambdaQueryWrapper<StudyBookTypes> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StudyBookTypes> buildQueryWrapper(StudyBookTypesBo bo) {
//        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudyBookTypes> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, StudyBookTypes::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getTypesName()), StudyBookTypes::getTypesName, bo.getTypesName());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(StudyBookTypesBo bo) {
        StudyBookTypes add = BeanUtil.toBean(bo, StudyBookTypes.class);
//        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(StudyBookTypesBo bo) {
        StudyBookTypes update = BeanUtil.toBean(bo, StudyBookTypes.class);
//        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StudyBookTypes entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}

